#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "question.h"
#define BUF_SIZE 8
/*
*************************函数功能：验证fgets和gets的区别****************************
*/
void main()
{
    int answer = -1;
    struct iobuf
    {
        char buf[BUF_SIZE];
        char others[BUF_SIZE];
    } buffer; //匿名的缓冲区结构体
    do
    {
        //初始化结构体
        memset(&buffer, '\0', sizeof(buffer));
        answer = get_question_answer("do you want to use fgets?");
        fprintf(stdout, "please input a line:\n");
        if (answer)
        {
            //使用fgets
            do
            {
                fgets(buffer.buf, BUF_SIZE, stdin);
                fprintf(stdout, "fgets() get string is %s\n", buffer.buf);
            } while (buffer.buf[strlen(buffer.buf) - 1] != '\n'); //读取未读入的输入
        }
        else
        {
            //不使用fgets
            //这种情况会产生溢出
            gets(buffer.buf);
            fprintf(stdout, "gets() get string is %s.\n", buffer.buf);
        }
        //打印溢出内容
        fprintf(stdout, "the others is %s.\n", buffer.others);
    } while (get_question_answer("continue?"));
}